package controll;

import java.awt.Color;

import javax.swing.*;
import verfahren.*;
import view.*;

public class SchedulingSimulatorController {

	private SchedulingVerfahren scheduler;
	private SchedulingSimulatorViewer viewer;

	public SchedulingSimulatorController() {
		viewer = new SchedulingSimulatorViewer(this);
	}

	public void play(SchedulingVerfahren sv) {
		if(sv instanceof FirstComeFirstServed){
			scheduler = (FirstComeFirstServed)sv;
			scheduler.erzeugeThreads();		
			scheduler.verwalteThreads();
		}
		else if(sv instanceof LongestJobFirst){
			scheduler = (LongestJobFirst)sv;
			scheduler.erzeugeThreads();		
			scheduler.verwalteThreads();
		}

		else if(sv instanceof PrioritaetgesteuertesScheduling){
			scheduler = (PrioritaetgesteuertesScheduling)sv;
			scheduler.erzeugeThreads();		
			scheduler.verwalteThreads();
		}
		else if(sv instanceof RoundRobin){
			scheduler = (RoundRobin)sv;
			scheduler.erzeugeThreads();		
			scheduler.verwalteThreads();
		}
		else if(sv instanceof ShortestJobFirst){
			scheduler = (ShortestJobFirst)sv;
			scheduler.erzeugeThreads();		
			scheduler.verwalteThreads();
		}
		else if(sv instanceof ShortestRemainingTimeFirst){
			scheduler = (ShortestRemainingTimeFirst)sv;
		}
		else if(sv instanceof LongestRemainingTimeFirst){
			scheduler = (LongestRemainingTimeFirst)sv;
		}
	}

	public static void main(final String[] args) {
		Runnable gui = new Runnable() {

			@Override
			public void run() {
				new SchedulingSimulatorController();
			}

		};

		SwingUtilities.invokeLater(gui);		//GUI anzeigen
	}

	public double getLaufZeit(){
		if(scheduler instanceof FirstComeFirstServed)
			return ((FirstComeFirstServed)scheduler).berechneLaufZeit();
		else if(scheduler instanceof LongestJobFirst)
			return((LongestJobFirst)scheduler).berechneLaufZeit();
		else if(scheduler instanceof PrioritaetgesteuertesScheduling)
			return((PrioritaetgesteuertesScheduling)scheduler).berechneLaufZeit();
		else if(scheduler instanceof RoundRobin)
			return ((RoundRobin)scheduler).berechneLaufZeit();
		else if(scheduler instanceof ShortestJobFirst)
			return((ShortestJobFirst)scheduler).berechneLaufZeit();
		else if(scheduler instanceof ShortestRemainingTimeFirst)
			return((ShortestRemainingTimeFirst)scheduler).berechneLaufZeit();
		else if(scheduler instanceof LongestRemainingTimeFirst)
			return((LongestRemainingTimeFirst)scheduler).berechneLaufZeit();

		return scheduler.berechneLaufZeit();
	}
	public double getWartezeit(){
		if(scheduler instanceof FirstComeFirstServed)
			return ((FirstComeFirstServed)scheduler).berechneWartezeit();
		else if(scheduler instanceof LongestJobFirst)
			return ((LongestJobFirst)scheduler).berechneWartezeit();
		else if(scheduler instanceof PrioritaetgesteuertesScheduling)
			return ((PrioritaetgesteuertesScheduling)scheduler).berechneWartezeit();
		else if(scheduler instanceof RoundRobin)
			return ((RoundRobin)scheduler).berechneWartezeit();
		else if(scheduler instanceof ShortestJobFirst)
			return ((ShortestJobFirst)scheduler).berechneWartezeit();
		else if(scheduler instanceof ShortestRemainingTimeFirst)
			return ((ShortestRemainingTimeFirst)scheduler).berechneWartezeit();
		else if(scheduler instanceof LongestRemainingTimeFirst)
			return ((LongestRemainingTimeFirst)scheduler).berechneWartezeit();

		return scheduler.berechneWartezeit();
	}

	public SchedulingSimulatorViewer getView() {
		return  viewer;
	}
	
	public Color getColor(int id) {
		return scheduler.getColor(id);
	}

}
